package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.auth.api.UserAccountServiceFeign;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venuebooking.demand.SaleClueGroup;
import com.eastfair.venuebooking.dto.*;
import com.eastfair.venuebooking.entity.SaleClue;
import com.eastfair.venuebooking.enums.SaleClueStateEnum;
import com.eastfair.venuebooking.service.SaleClueService;
import com.eastfair.venuebooking.vo.SaleClueVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 销售线索表
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/saleClue")
@Api(value = "SaleClue", tags = "销售线索表")
@PreAuth(replace = "venuebooking:saleClue:")
public class SaleClueController extends SuperController<SaleClueService, Long, SaleClue, SaleCluePageQuery, SaleClueSaveDTO, SaleClueUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Resource
    private SaleClueService saleClueService;

    @Resource
    private UserAccountServiceFeign userAccountServiceFeign;

    @Override
    public void handlerResult(IPage<SaleClue> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<SaleClue> saleClueList = list.stream().map((map) -> {
            SaleClue saleClue = SaleClue.builder().build();
            //TODO 请在这里完成转换
            return saleClue;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(saleClueList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<SaleCluePageQuery> params) {
        SaleCluePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询销售线索.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/saleClue")
    @BaseControllerInfo(voGroup = {SaleClueGroup.QuerySaleClue.class})
    public R<IPage<SaleClueVO>> querySaleClue(@RequestBody PageParams<SaleClueQuery> pageParams) {
        log.info("pageSaleClue - 分页查询销售线索, pageParams={}", pageParams);
        return R.successDef(saleClueService.listSaleClueVOs(pageParams));
    }

    /**
     * 分页查询销售线索.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/normal/saleClue")
    @BaseControllerInfo(voGroup = {SaleClueGroup.QuerySaleClue.class})
    public R<IPage<SaleClueVO>> queryNormalSaleClue(@RequestBody PageParams<SaleClueQuery> pageParams) {
        log.info("pageSaleClue - 分页查询销售线索, pageParams={}", pageParams);

        SaleClueQuery model = pageParams.getModel();
        model.setState(SaleClueStateEnum.NORMAL.getCode());
        return R.successDef(saleClueService.listSaleClueVOs(pageParams));
    }

    /**
     * 分页查询我的销售线索.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/me/saleClue")
    @BaseControllerInfo(voGroup = {SaleClueGroup.QuerySaleClue.class})
    public R<IPage<SaleClueVO>> queryMeSaleClue(@RequestBody PageParams<SaleClueQuery> pageParams) {
        log.info("pageSaleClue - 分页查询我的销售线索, pageParams={}", pageParams);

        SaleClueQuery model = pageParams.getModel();
        model.setBelongTo(ContextUtil.getUserId());
        model.setState(SaleClueStateEnum.NORMAL.getCode());
        return R.successDef(saleClueService.listSaleClueVOs(pageParams));
    }

    /**
     * 分页查询公共销售线索.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/public/saleClue")
    @BaseControllerInfo(voGroup = {SaleClueGroup.QuerySaleClue.class})
    public R<IPage<SaleClueVO>> queryPublicSaleClue(@RequestBody PageParams<SaleClueQuery> pageParams) {
        log.info("pageSaleClue - 分页查询公共销售线索, pageParams={}", pageParams);

        SaleClueQuery model = pageParams.getModel();
        model.setState(SaleClueStateEnum.PUBLIC.getCode());
        return R.successDef(saleClueService.listSaleClueVOs(pageParams));
    }

    /**
     * 查询详情.
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/get/{id}")
    public R<SaleClueVO> getById(@PathVariable("id") Long id) {
        log.info("getById - 查询详情, id={}", id);

        return R.successDef(saleClueService.getSaleClueVOById(id));
    }

    /**
     * 保存销售线索.
     *
     * @param saleClueDTO the sale clue dto
     * @return the r
     */
    @SysLog("保存销售线索")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody SaleClueDTO saleClueDTO) {
        log.info("saveSaleClue - 保存销售线索, saleClueDTO={}", saleClueDTO);
        return R.successDef(saleClueService.saveSaleClue(saleClueDTO));
    }

    /**
     * 更新销售线索.
     *
     * @param saleClueDTO the sale clue dto
     * @return the r
     */
    @SysLog("更新销售线索")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody SaleClueDTO saleClueDTO) {
        log.info("updateSaleClue - 更新销售线索, saleClueDTO={}", saleClueDTO);
        return R.successDef(saleClueService.updateSaleClue(saleClueDTO));
    }

    /**
     * 移入公海.
     *
     * @param publicDTO the update dto list
     * @return the r
     */
    @SysLog("移入公海")
    @PostMapping("/in/public")
    public R<Boolean> updateInPublic(@RequestBody @Validated({SaleClueDTO.Public.class}) SaleCluePublicDTO publicDTO) {
        log.info("movePublic - 移入公海, publicDTO={}", publicDTO);

        return R.successDef(saleClueService.updatePublic(publicDTO));
    }

    /**
     * 分配线索.
     *
     * @param assignDTO the assign dto
     * @return the r
     */
    @SysLog("分配线索")
    @PostMapping("/assign")
    public R<Boolean> updateAssign(@RequestBody @Validated({SaleClueDTO.Assign.class}) SaleClueAssignDTO assignDTO) {
        log.info("assign - 分配线索, assignDTO={}", assignDTO);

        return R.successDef(saleClueService.updateBelongTo(assignDTO));
    }

    /**
     * 领取线索.
     *
     * @param list the ids
     * @return the r
     */
    @SysLog("领取线索")
    @PostMapping("/take")
    public R<Boolean> updateTake(@RequestBody @NotEmpty(message = "领取列表不可为空") List<SaleClueAssignDTO> list) {
        log.info("take - 领取线索, list={}", list);
        return R.successDef(saleClueService.updateBelongTo(list));
    }

    /**
     * 更新线索状态.
     *
     * @param updateDTO the update dto
     * @return the r
     */
    @SysLog("修改线索状态")
    @PostMapping("/update/clueStatus")
    public R<Boolean> updateClueStatus(@RequestBody @Validated({SaleClueDTO.UpdateSaleClueStatus.class})
                                       SaleClueDTO updateDTO) {
        log.info("updateClueStatus - 更新状态, updateDTO={}", updateDTO);

        return R.successDef(saleClueService.updateClueStatus(updateDTO));
    }

    /**
     * 移除线索.
     *
     * @param ids the ids
     * @return the r
     */
    @SysLog("移除线索")
    @PostMapping("/remove")
    public R<Boolean> remove(@RequestBody List<Long> ids) {
        log.info("remove - 移除线索, ids={}", ids);
        return R.successDef(saleClueService.removeByIds(ids));
    }
}
